package com.ddwl.goods.dao.bargain;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.mapper.bargain.BargainSkuMapper;
import com.ddwl.goods.model.bargain.BargainSku;
import com.ddwl.schema.vo.goods.bargain.BargainSkuVo;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * @author: hemiaojun
 * @Date: 2020/10/28
 * @Description:
 */
@Repository
public class BargainSkuDao extends TkDao<BargainSkuMapper, BargainSku> {

    @Resource
    private BargainSkuMapper bargainSkuMapper;

    public void deleteByActivityId(String activityId) {
        Example example=getExample();
        example.and().andEqualTo("activityId",activityId);
        bargainSkuMapper.deleteByExample(example);
    }

    public void updateDflagByActivityId(String activityId) {
        Example example=getExample();
        example.and().andEqualTo("activityId",activityId)
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode());
        BargainSku entity = new BargainSku();
        entity.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        entity.setGmtModified(Instant.now());
        bargainSkuMapper.updateByExampleSelective(entity,example);
    }

    public BargainSku selectByBarCode(String barcode,String activityId) {
        BargainSku entity = new BargainSku();
        entity.setBarcode(barcode);
        entity.setActivityId(activityId);
        return selectOne(entity);
    }

    public BargainSkuVo getBargainSkuVo(String barcode) {
        BargainSkuVo vo= bargainSkuMapper.getBargainSkuVo(barcode);
        vo.setPurchaseNum(1);
        return vo;
    }

    public void batchUpdateSales(List<BargainSku> bsList) {
        bargainSkuMapper.batchUpdateSales(bsList);
    }
}
